package org.opentcs.kcvehicle.communication.kc.udp.Service;

import java.util.ArrayList;
import java.util.List;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.AgvEvent;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.AgvEventConstant;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.b1.SubscribeInfo;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.b1.SubscribeParam;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.b1.SubscribeRsp;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.rsp.RcvEventPackage;
import org.opentcs.kcvehicle.communication.kc.udp.io.UDPClient;

public class SubRobotStatue
    extends
      BaseCommand {

  /**
   * 上报时间间隔
   */
  public static final int intervalTime = 500;
  /**
   * 订阅时长
   */
  public static int subDuration = intervalTime * 5;

  /**
   * decs: 下发订阅信息
   * 指令：0xB1
   * author: caixiang
   * date: 2025/1/17 16:25
   */
  public static AgvEvent issueSubscribe() {
    List<SubscribeInfo> subscribeInfoList = new ArrayList<>();
    SubscribeInfo subscribeInfo = new SubscribeInfo(
        new byte[]{(byte) 0xaf, (byte) 0x00}, (short) intervalTime, subDuration
    );
//    SubscribeInfo subscribeInfo = new SubscribeInfo(new byte[]{(byte)0xb0,(byte)0x00}, (short) 100,1000);
    subscribeInfoList.add(subscribeInfo);
    SubscribeParam subscribeParam = new SubscribeParam(subscribeInfoList);
    AgvEvent agvEvent = new AgvEvent(
        AgvEventConstant.CommandCode_ISSUE_SUBSCRIPTION, subscribeParam.toBytes()
    );
    return agvEvent;
  }

  public static void command() {
//    0xB1(订阅信息)
    AgvEvent agvEvent = issueSubscribe();
    printInfo(agvEvent);
    //todo 订阅参数构建完毕 去写 回调部分
    RcvEventPackage rcv = UDPClient.localAGV.send(agvEvent);
    if (rcv.isOk()) {
      System.out.println();
      System.out.println("received transationId : " + "isok:" + rcv.isOk());
      SubscribeRsp subscribeRsp = new SubscribeRsp(rcv.getDataBytes());
      if (subscribeRsp.isOk()) {
        //...
      }
      else {
        //...
      }
    }
    else {
      System.out.println();
      System.out.println("received transationId : " + "isok:" + rcv.isOk());
    }
  }
}
